//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ruoyi.live.exam.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.live.base.domain.LiveScoreStandard;
import com.ruoyi.live.base.mapper.LiveScoreStandardMapper;
import com.ruoyi.live.tool.commons.ICommons;
import com.ruoyi.live.tool.commons.IConvert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;

@Service
public class LiveScoreStandardService {
    private static final Logger log = LoggerFactory.getLogger(LiveScoreStandardService.class);
    @Autowired
    LiveScoreStandardMapper dao;
    @Autowired
    JdbcTemplate jdbcTemplate;

    public LiveScoreStandardService() {
    }

    public void addLiveScoreStandard(LiveScoreStandard entity) throws Exception {
        try {
            entity.setState(1);
            this.dao.insert(entity);
        } catch (Exception var3) {
            var3.printStackTrace();
            log.error("新增LiveScoreStandard失败,编号LiveScoreStandardService-addLiveScoreStandard-1");
            throw new Exception("新增失败,编号LiveScoreStandardService-addLiveScoreStandard-1");
        }
    }

    @CacheEvict(
            value = {"standardByCode"},
            key = "#entity.standardCode"
    )
    public void updateLiveScoreStandard(LiveScoreStandard entity) throws Exception {
        try {
            this.dao.updateById(entity);
        } catch (Exception var3) {
            var3.printStackTrace();
            log.error("更新LiveScoreStandard失败,编号LiveScoreStandardService-updateLiveScoreStandard-1,id:{}", entity.getId());
            throw new Exception("更新失败,编号LiveScoreStandardService-updateLiveScoreStandard-1");
        }
    }

    public void updateLiveScoreStandardNoCode(LiveScoreStandard entity) throws Exception {
        try {
            this.dao.updateById(entity);
        } catch (Exception var3) {
            var3.printStackTrace();
            log.error("更新LiveScoreStandard失败,编号LiveScoreStandardService-updateLiveScoreStandard-1,id:{}", entity.getId());
            throw new Exception("更新失败,编号LiveScoreStandardService-updateLiveScoreStandard-1");
        }
    }

    public LiveScoreStandard getLiveScoreStandard(Long id) {
        return (LiveScoreStandard)this.dao.selectById(id);
    }

    public int deleteLiveScoreStandard(String[] ids) throws Exception {
        if (ids.length == 0) {
            throw new Exception("请选择要删除的数据,编号LiveScoreStandardService-deleteLiveScoreStandard-1");
        } else {
            try {
                int count = 0;

                for(int i = 0; i < ids.length; ++i) {
                    if (!ICommons.isNullOrEmpty(ids[i])) {
                        count += this.dao.deleteById(IConvert.string2long(ids[i]));
                    }
                }

                return count;
            } catch (Exception var4) {
                var4.printStackTrace();
                log.error("删除LiveScoreStandard失败,编号LiveScoreStandardService-deleteLiveScoreStandard-2,ids:{}", ids.toString());
                throw new Exception("删除的数据失败,编号LiveScoreStandardService-deleteLiveScoreStandard-2");
            }
        }
    }

    public List<LiveScoreStandard> query(LiveScoreStandard entity) {
        QueryWrapper<LiveScoreStandard> wrapper = (QueryWrapper)((QueryWrapper)((QueryWrapper)((QueryWrapper)((QueryWrapper)((QueryWrapper)((QueryWrapper)((QueryWrapper)((QueryWrapper)((QueryWrapper)((QueryWrapper)(new QueryWrapper()).eq(!ICommons.isNullOrEmpty(entity.getId()), "id", entity.getId())).like(!ICommons.isNullOrEmpty(entity.getStandardCode()), "standard_code", entity.getStandardCode())).like(!ICommons.isNullOrEmpty(entity.getStandardName()), "standard_name", entity.getStandardName())).eq(!ICommons.isNullOrEmpty(entity.getStandardType()), "standard_type", entity.getStandardType())).eq(!ICommons.isNullOrEmpty(entity.getStandardValue()), "standard_value", entity.getStandardValue())).eq(!ICommons.isNullOrEmpty(entity.getOrders()), "orders", entity.getOrders())).eq(!ICommons.isNullOrEmpty(entity.getState()), "state", entity.getState())).eq(!ICommons.isNullOrEmpty(entity.getParentId()), "parent_id", entity.getParentId())).eq(!ICommons.isNullOrEmpty(entity.getScoreType()), "score_type", entity.getScoreType())).eq(!ICommons.isNullOrEmpty(entity.getScoreStep()), "score_step", entity.getScoreStep())).eq(!ICommons.isNullOrEmpty(entity.getScoreScope()), "score_scope", entity.getScoreScope());
        return this.dao.selectList(wrapper);
    }

    public List<LiveScoreStandard> list(Integer type) {
        List<LiveScoreStandard> list = this.dao.selectList((Wrapper)(new QueryWrapper((new LiveScoreStandard()).setState(1).setStandardType(type).setParentId(0L))).orderByAsc("orders"));
        Iterator var3 = list.iterator();

        while(var3.hasNext()) {
            LiveScoreStandard parent = (LiveScoreStandard)var3.next();
            parent.setStandards(this.dao.selectList((Wrapper)(new QueryWrapper((new LiveScoreStandard()).setState(1).setParentId(parent.getId()))).orderByAsc("orders")));
        }

        return list;
    }

    public Integer getOrders(Integer standardType, Long parentId) {
        String sql = "select ifnull(max(orders),0) + 1 orders from live_score_standard where parent_id = ? and standard_type = ?";
        if (ICommons.isNullOrEmpty(parentId)) {
            parentId = 0L;
        }

        return (Integer)this.jdbcTemplate.queryForObject(sql, Integer.class, new Object[]{parentId, standardType});
    }
}
